/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.tor.tribes.types;

import de.tor.tribes.reportserver.PersistenceImpl;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

/**
 *
 * @author Torridity
 */
@Entity
@Table(name = "ReportGroups")
public class ZebraReportGroup implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    @OneToOne
    private ZebraUser owner;
    @OneToMany(mappedBy = "group", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<ZebraReportGroupMapping> reports;

    public ZebraReportGroup() {
        reports = new HashSet<ZebraReportGroupMapping>();
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the reports
     */
    public Set<ZebraReportGroupMapping> getReports() {
        return reports;
    }

    /**
     * @param reports the reports to set
     */
    public void setReports(Set<ZebraReportGroupMapping> reports) {
        this.reports = reports;
    }

    public ZebraUser getOwner() {
        return owner;
    }

    public void setOwner(ZebraUser owner) {
        this.owner = owner;
    }

    public void addReport(ZebraReportAccessGrant pGrant, PersistenceImpl pPersistence) {
        if (pGrant.getGroupMapping() != null) {
            pGrant.getGroupMapping().getGroup().removeReport(pGrant, pPersistence);
        }
        ZebraReportGroupMapping mapping = new ZebraReportGroupMapping();
        mapping.setGroup(this);
        mapping.setGrant(pGrant);
        pPersistence.persist(mapping);
        reports.add(mapping);
        pGrant.setGroupMapping(mapping);
    }

    public void removeReport(ZebraReportAccessGrant pReport, PersistenceImpl pPersistence) {
        ZebraReportGroupMapping mapping = pReport.getGroupMapping();
        pPersistence.remove(mapping);
        pReport.setGroupMapping(null);
        reports.remove(mapping);
    }

    public void delete(PersistenceImpl pPersistence) {
        for (ZebraReportGroupMapping mapping : getReports().toArray(new ZebraReportGroupMapping[]{})) {
            ZebraReportAccessGrant grant = mapping.getGrant();
            grant.delete(pPersistence);
        }

        pPersistence.remove(this);
    }
}
